﻿using Commonality.Model;
using HalconDotNet;
using Separate.BLL;
using Separate.Model;
using Separate.Model.Vision;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WZS_Auto_Classifying_Separate
{
    public partial class MainForm : Form
    {
        public static MainForm mainForm;
        private LogForm logForm;
        private ConfigForm configForm;
        /// <summary>
        /// 关机标志
        /// </summary>
        public static bool ThisClose;
        public MainForm()
        {
            InitializeComponent();
            mainForm = this;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            logForm = new LogForm();
            configForm = new ConfigForm();
            SeparateAutoMain.visionWindowA = new Separate.BLL.VisionWindow("CameraA");
            SeparateAutoMain.visionWindowB = new Separate.BLL.VisionWindow("CameraB");
            SeparateAutoMain.ngInfoForm = new Separate.BLL.InfoForm.NgInfoForm();
            SeparateAutoMain.InOutputForm = new InOutputForm();
            InitForm();
            ThisClose = false;
            bool PLCOpenFlag = SeparateAutoMain.SeparateStart();
            OpenPLCDis(PLCOpenFlag);
            OpenCamera();

            ProjectVersion.Text = GObject.ProjectVersion.ToString();
        }
        private void InitForm()
        {
            //
            logForm.FormBorderStyle = FormBorderStyle.None;
            logForm.Size = panelLog.Size;
            logForm.BackColor = this.BackColor;
            logForm.TopLevel = false;
            logForm.Dock = DockStyle.Fill;
            panelLog.Controls.Add(logForm);
            logForm.Show();
            //
            configForm.FormBorderStyle = FormBorderStyle.None;
            configForm.Size = ConfigPanel.Size;
            configForm.BackColor = this.BackColor;
            configForm.TopLevel = false;
            configForm.Dock = DockStyle.Fill;
            ConfigPanel.Controls.Add(configForm);
            configForm.Show();
            //
            SeparateAutoMain.visionWindowA.FormBorderStyle = FormBorderStyle.None;
            SeparateAutoMain.visionWindowA.Size = VisionWindowPanelA.Size;
            SeparateAutoMain.visionWindowA.BackColor = this.BackColor;
            SeparateAutoMain.visionWindowA.TopLevel = false;
            SeparateAutoMain.visionWindowA.Dock = DockStyle.Fill;
            VisionWindowPanelA.Controls.Add(SeparateAutoMain.visionWindowA);
            SeparateAutoMain.visionWindowA.Show();
            //
            SeparateAutoMain.visionWindowB.FormBorderStyle = FormBorderStyle.None;
            SeparateAutoMain.visionWindowB.Size = VisionWindowPanelB.Size;
            SeparateAutoMain.visionWindowB.BackColor = this.BackColor;
            SeparateAutoMain.visionWindowB.TopLevel = false;
            SeparateAutoMain.visionWindowB.Dock = DockStyle.Fill;
            VisionWindowPanelB.Controls.Add(SeparateAutoMain.visionWindowB);
            SeparateAutoMain.visionWindowB.Show();
            //
            SeparateAutoMain.ngInfoForm.FormBorderStyle = FormBorderStyle.None;
            SeparateAutoMain.ngInfoForm.Size = SQRCPanel.Size;
            SeparateAutoMain.ngInfoForm.BackColor = this.BackColor;
            SeparateAutoMain.ngInfoForm.TopLevel = false;
            SeparateAutoMain.ngInfoForm.Dock = DockStyle.Fill;
            SQRCPanel.Controls.Add(SeparateAutoMain.ngInfoForm);
            SeparateAutoMain.ngInfoForm.Show();
            //
            SeparateAutoMain.InOutputForm.FormBorderStyle = FormBorderStyle.None;
            SeparateAutoMain.InOutputForm.Size = StatisticsPanel.Size;
            SeparateAutoMain.InOutputForm.BackColor = this.BackColor;
            SeparateAutoMain.InOutputForm.TopLevel = false;
            SeparateAutoMain.InOutputForm.Dock = DockStyle.Fill;
            StatisticsPanel.Controls.Add(SeparateAutoMain.InOutputForm);
            SeparateAutoMain.InOutputForm.Show();

        }
        private void OpenCamera()
        {
            try
            {
                if (SeparateAutoMain.visionWindowA.OpenCamera())
                {
                    SeparateAutoMain.visionWindowA.Camera.SetExposureTimeAbs(18000);
                    Log4Net.MsgInfo("打开相机A");
                    CameraTool.BackColor = Color.Green;
                }
                else
                {
                    Log4Net.MsgInfo("未打开相机A");
                    CameraTool.BackColor = Color.Red;
                    CameraTool.Text = "未打开相机A";
                }
            }
            catch (Exception)
            {

                Log4Net.MsgInfo("打开相机A失败");
                CameraTool.BackColor = Color.Red;
                CameraTool.Text = "打开相机A失败";
            }

            try
            {
                if (SeparateAutoMain.visionWindowB.OpenCamera())
                {
                    SeparateAutoMain.visionWindowB.Camera.SetExposureTimeAbs(18000);
                    Log4Net.MsgInfo("打开相机B");
                    CameraTool.BackColor = Color.Green;
                }
                else
                {
                    Log4Net.MsgInfo("未打开相机B");
                    CameraTool.BackColor = Color.Red;
                    CameraTool.Text = "未打开相机B";
                }
            }
            catch (Exception)
            {
                Log4Net.MsgInfo("打开相机B失败");
                CameraTool.BackColor = Color.Red;
                CameraTool.Text = "打开相机B失败";
            }
        }
        private void OpenPLCDis(bool open)
        {
            if (open)
            {
                StartErr.BackColor = Color.Green;
                StartErr.Text = "PLC连接";
            }
            else
            {
                StartErr.BackColor = Color.Red;
                StartErr.Text = "PLC未连接";
            }
        }
        private void OpenConfigPage_Click(object sender, EventArgs e)
        {
            ConfigPanel.BringToFront();
            if (!configForm.Visible)
            {
                foreach (var item in ConfigPanel.Controls)
                {
                    if (item is Form)
                    {
                        ((Form)item).Visible = false;
                    }
                }
                configForm.Visible = true;
            }
            Log4Net.MsgInfo("进入Config页");
        }

        private void Debug_Click(object sender, EventArgs e)
        {
            DebugForm debugForm = new DebugForm();
            debugForm.Show();
        }

        private void OpenVisionPage_Click(object sender, EventArgs e)
        {
            HomePanel.BringToFront();
            if (!SeparateAutoMain.visionWindowA.Visible)
            {
                foreach (var item in SeparateAutoMain.visionWindowA.Controls)
                {
                    if (item is Form)
                    {
                        ((Form)item).Visible = false;
                    }
                }
                SeparateAutoMain.visionWindowA.Visible = true;
            }
            if (!SeparateAutoMain.visionWindowB.Visible)
            {
                foreach (var item in SeparateAutoMain.visionWindowB.Controls)
                {
                    if (item is Form)
                    {
                        ((Form)item).Visible = false;
                    }
                }
                SeparateAutoMain.visionWindowB.Visible = true;
            }
            Log4Net.MsgInfo("进入Home页");
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Separate.BLL.SeparateAutoMain.AutoMainLoop();
            //SeparateAutoMain.SeparateStop();
            SeparateAutoMain.ThisClose = true;
            DialogResult result = MessageBox.Show("软件中断中\r\n你确定要关闭吗！", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                e.Cancel = false;  //点击OK   
                SeparateAutoMain.visionWindowA.CloseCamera();
                SeparateAutoMain.visionWindowB.CloseCamera();
                SeparateAutoMain.SeparateStop();
            }
            else
            {
                SeparateAutoMain.ThisClose = false;
                e.Cancel = true;
            }
        }
        private void OpenPageButtColo(object sender, MouseEventArgs e)
        {
            ComponentResourceManager resources = new ComponentResourceManager(typeof(Properties.Resources)); ;
            switch ((sender as Button).Name)
            {
                case "OpenVisionPage":
                    OpenVisionPage.BackgroundImage = ((Image)(resources.GetObject("按键2-01")));
                    OpenVisionPage.ForeColor = Color.Black;
                    OpenConfigPage.BackgroundImage = ((Image)(resources.GetObject("按键1-01")));
                    OpenConfigPage.ForeColor = Color.FromArgb(255, 128, 0);
                    StatisticsPagBut.BackgroundImage = ((Image)(resources.GetObject("按键1-01")));
                    StatisticsPagBut.ForeColor = Color.FromArgb(255, 128, 0);
                    break;
                case "OpenConfigPage":
                    OpenConfigPage.BackgroundImage = ((Image)(resources.GetObject("按键2-01")));
                    OpenConfigPage.ForeColor = Color.Black;
                    OpenVisionPage.BackgroundImage = ((Image)(resources.GetObject("按键1-01")));
                    OpenVisionPage.ForeColor = Color.FromArgb(255, 128, 0);
                    StatisticsPagBut.BackgroundImage = ((Image)(resources.GetObject("按键1-01")));
                    StatisticsPagBut.ForeColor = Color.FromArgb(255, 128, 0);
                    break;
                case "StatisticsPagBut":
                    StatisticsPagBut.BackgroundImage = ((Image)(resources.GetObject("按键2-01")));
                    StatisticsPagBut.ForeColor = Color.Black;
                    OpenVisionPage.BackgroundImage = ((Image)(resources.GetObject("按键1-01")));
                    OpenVisionPage.ForeColor = Color.FromArgb(255, 128, 0);
                    OpenConfigPage.BackgroundImage = ((Image)(resources.GetObject("按键1-01")));
                    OpenConfigPage.ForeColor = Color.FromArgb(255, 128, 0);
                    break;
                default:
                    break;
            }
        }

        private void StatisticsPagBut_Click(object sender, EventArgs e)
        {
            StatisticsPanel.BringToFront();
            if (!SeparateAutoMain.InOutputForm.Visible)
            {
                foreach (var item in StatisticsPanel.Controls)
                {
                    if (item is Form)
                    {
                        ((Form)item).Visible = false;
                    }
                }
                SeparateAutoMain.InOutputForm.Visible = true;
            }
            Log4Net.MsgInfo("进入统计页");

            SeparateAutoMain.InOutputForm.ChartDisply();
        }
    }
}
